package com.softtown.modules.dapai.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompServiceOrderDao;
import com.softtown.modules.dapai.dto.CompServiceOrderAPPDTO;
import com.softtown.modules.dapai.dto.CompServiceOrderAddDTO;
import com.softtown.modules.dapai.dto.CompServiceOrderDTO;
import com.softtown.modules.dapai.entity.CompServiceOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompServiceOrderEntity;
import com.softtown.modules.dapai.service.CompServiceOrderDetailService;
import com.softtown.modules.dapai.service.CompServiceOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 维修服务单
* @date: 2022/1/2 11:38
* @Param:
* @return:
*/

@Service
public class CompServiceOrderServiceImpl extends
        BaseServiceImpl<CompServiceOrderDao, CompServiceOrderEntity> implements CompServiceOrderService {


    @Resource
    private CompServiceOrderDetailService compServiceOrderDetailService;


    @Override
    public PageInfo<CompServiceOrderDTO> getServiceOrderPageList(CompServiceOrderDTO compServiceOrderDTO) {
        Integer pageSize = compServiceOrderDTO.getPageSize();
        Integer page = compServiceOrderDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompServiceOrderDTO> compServiceOrderDTOList =
                baseDao.getServiceOrderPageList(compServiceOrderDTO.getCompId(), compServiceOrderDTO.getParam());
        return  new PageInfo<>(compServiceOrderDTOList);
    }

    @Override
    public CompServiceOrderAddDTO getServiceOrderById(Long compId, Long id) {
        CompServiceOrderAddDTO compServiceOrderAddDTO = new CompServiceOrderAddDTO();
        compServiceOrderAddDTO.setCompServiceOrderEntity(baseDao.getServiceOrderById(compId, id));
        compServiceOrderAddDTO.setCompServiceOrderDetailEntityList(baseDao.getServiceOrderDetailList(compId, id));
        return compServiceOrderAddDTO;
    }

    @Override
    public PageInfo<CompServiceOrderAPPDTO> getServiceOrderPageListAPP(CompServiceOrderAPPDTO compServiceOrderAPPDTO) {
        Integer pageSize = compServiceOrderAPPDTO.getPageSize();
        Integer page = compServiceOrderAPPDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompServiceOrderAPPDTO> compServiceOrderAPPDTOList =
                baseDao.getServiceOrderPageListAPP(compServiceOrderAPPDTO.getCompId(), compServiceOrderAPPDTO.getParam());
        return  new PageInfo<>(compServiceOrderAPPDTOList);
    }

    @Override
    public CompServiceOrderAPPDTO getServiceOrderByIdAPP(Long compId, Long id) {
        CompServiceOrderAPPDTO compServiceOrderAPPDTO = baseDao.getServiceOrderByIdAPP(compId, id);
        return compServiceOrderAPPDTO;
    }

    @Override
    @Transactional
    public void submitServiceOrderAPP(CompServiceOrderAPPDTO compServiceOrderAPPDTO) {
        baseDao.submitServiceOrderAPP(compServiceOrderAPPDTO.getCompId(), compServiceOrderAPPDTO.getId(), compServiceOrderAPPDTO.getDistribution());
        if (1 == compServiceOrderAPPDTO.getDistribution()){
            baseDao.updateAfterSaleOrderDistributionAPP(compServiceOrderAPPDTO.getCompId(), compServiceOrderAPPDTO.getAfterSaleAppId(), compServiceOrderAPPDTO.getDistribution());
        }
    }

    @Override
    @Transactional
    public void addServiceOrder(CompServiceOrderAddDTO CompServiceOrderAddDTO) {
        CompServiceOrderEntity compServiceOrderEntity =ConvertUtils
                .sourceToTarget(CompServiceOrderAddDTO.getCompServiceOrderEntity(), CompServiceOrderEntity.class);
        insert(compServiceOrderEntity);
        CompServiceOrderAddDTO.getCompServiceOrderDetailEntityList().forEach(item ->{
            item.setServiceOrderId(compServiceOrderEntity.getId());
            item.setCompId(compServiceOrderEntity.getCompId());
        });
        compServiceOrderDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompServiceOrderAddDTO.getCompServiceOrderDetailEntityList(), CompServiceOrderDetailEntity.class));

    }

    @Override
    @Transactional
    public void updateServiceOrder(CompServiceOrderAddDTO CompServiceOrderAddDTO) {
        CompServiceOrderEntity compServiceOrderEntity =ConvertUtils
                .sourceToTarget(CompServiceOrderAddDTO.getCompServiceOrderEntity(), CompServiceOrderEntity.class);
        updateById(compServiceOrderEntity);
        baseDao.deleteDetail(compServiceOrderEntity.getCompId(), compServiceOrderEntity.getId());
        CompServiceOrderAddDTO.getCompServiceOrderDetailEntityList().forEach(item ->{
            item.setServiceOrderId(compServiceOrderEntity.getId());
            item.setCompId(compServiceOrderEntity.getCompId());
        });
        compServiceOrderDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompServiceOrderAddDTO.getCompServiceOrderDetailEntityList(), CompServiceOrderDetailEntity.class));
    }

    @Override
    public void deleteServiceOrder(CompServiceOrderDTO compServiceOrderDTO) {
        baseDao.deleteServiceOrder(compServiceOrderDTO.getCompId(), compServiceOrderDTO.getId());
        baseDao.deleteDetail(compServiceOrderDTO.getCompId(), compServiceOrderDTO.getId());
    }
}
